package com.example.servlet.type;

import com.alibaba.fastjson.JSON;
import com.example.service.TypeService;
import com.example.service.impl.TypeServiceImpl;
import com.example.vo.ResponseData;
import com.example.vo.ResultEnums;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加虚拟商品的分类
 */
@WebServlet("/CollectionServlet/addtype")
public class AddTypeServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json;charset=utf-8");
        String name = req.getParameter("name");
        TypeService typeService = new TypeServiceImpl();
        boolean result = typeService.save(name);
        ResponseData data = null;
        if(result){
             data = ResponseData.ok(ResultEnums.CREATED);
        }else{
             data = ResponseData.ok(ResultEnums.CREATED_FAIL);
        }
        resp.getWriter().println(JSON.toJSONString(data));
    }
}
